#!/usr/bin/python3
# -*- coding:utf-8 -*-
""""
@Author: shijianbo
@Email:
@Time: 2023/4/6 16:32
@File: basic.py
"""
import threading
from abc import ABCMeta, abstractmethod

from BladeFerm.Lib.Common import logger


class DatabaseUtil(metaclass=ABCMeta):

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        for cursor in self.CURSOR.values():
            cursor.close()
        self.db.close()

    def commit(self):
        self.db.commit()

    def rollback(self):
        self.db.rollback()

    @abstractmethod
    def __init__(self,user,password,ip,port,**kwargs):
        pass

    def execsql(self, sql, autoCommit=True,fetch=True):
        self.cursor.execute(sql)
        result=[]
        if sql.lower().startswith('select') and fetch:
            result += [dict(zip([desc[0] for desc in self.cursor.description],line)) for line in self.cursor.fetchall()]
        if autoCommit:
            self.db.commit()
        return result

    @property
    def cursor(self):
        _id = threading.get_ident()
        if _id not in self.CURSOR:
            self.CURSOR[_id]=self.db.cursor()
        return self.CURSOR[_id]

    def getJarFile(self):
        '''
        获取jarFile
        '''
        jarName = ['DmJdbcDriver18.jar', 'mysql-connector-java-8.0.29.jar', 'oceanbase-client-1.1.10.jar',
                 'ojdbc8.jar', 'postgresql-42.3.4.jar']
        # path=os.path.dirname(__file__)
        path = 'C:\qturbo-python\Python36-32'
        jarFile=[f"{path}\\{i}" for i in jarName]
        logger.debug(f'{jarFile}')
        return jarFile